package com.ruoyi.web.controller.prevention;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.PreventionCheckTaskConfigDTO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskCountVO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskVO;
import com.ruoyi.system.entity.PreventionCheckTask;
import com.ruoyi.system.entity.PreventionHiddenInfoRecord;
import com.ruoyi.system.entity.PreventionHiddenRegister;
import com.ruoyi.system.mapper.PreventionCheckTaskConfigMapper;
import com.ruoyi.system.mapper.PreventionCheckTaskMapper;
import com.ruoyi.system.mapper.PreventionHiddenInfoRecordMapper;
import com.ruoyi.system.service.PreventionCheckTaskService;
import com.ruoyi.system.service.PreventionHiddenRegisterService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.ruoyi.common.utils.DateToUtils.getBeginDayOfWeek;
import static com.ruoyi.common.utils.DateToUtils.getEndDayOfWeek;

/**
 * 排查任务表(PreventionCheckTask)表控制层
 * @since 2022-11-18 14:04:46
 */
@RestController
@RequestMapping("/preventionCheckTask")
public class PreventionCheckTaskController extends BaseController {

    @Resource
    private PreventionCheckTaskService preventionCheckTaskService;
    @Resource
    private PreventionCheckTaskMapper preventionCheckTaskMapper;
    @Resource
    private PreventionCheckTaskConfigMapper preventionCheckTaskConfigMapper;
    @Resource
    private PreventionHiddenRegisterService preventionHiddenRegisterService;
    @Autowired
    private PreventionHiddenInfoRecordMapper preventionHiddenInfoRecordMapper;

    DateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 分页查询
     * @param preventionCheckTask 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(PreventionCheckTask preventionCheckTask) {
        startPage();
        List<PreventionCheckTask> preventionCheckTasks = this.preventionCheckTaskService.queryByPage(preventionCheckTask);
        if (!CollectionUtils.isEmpty(preventionCheckTasks)){
            for (PreventionCheckTask checkTask : preventionCheckTasks) {
                String countHidden = preventionHiddenInfoRecordMapper.getTaskCountHidden(checkTask.getId());
                checkTask.setCountHidden(countHidden);
                PreventionHiddenInfoRecord hiddenInfoRecord = preventionHiddenInfoRecordMapper.getHiddenTaskDetails(checkTask.getCheckTaskConfigId(),checkTask.getId());
                if (!ObjectUtils.isEmpty(hiddenInfoRecord)){
                    checkTask.setCheckResult("有隐患");
                    this.preventionCheckTaskService.update(checkTask);
                }
            }
        }
        return getDataTable(preventionCheckTasks);
    }

    /**
     * 分页查询
     * @param preventionCheckTask 筛选条件
     * @return 查询结果
     */
    @GetMapping("/listApp")
    public TableDataInfo listApp(PreventionCheckTask preventionCheckTask) {
        startPage();
        List<PreventionCheckTask> preventionCheckTasks = this.preventionCheckTaskService.queryByPageApp(preventionCheckTask);
        if (!CollectionUtils.isEmpty(preventionCheckTasks)){
            for (PreventionCheckTask checkTask : preventionCheckTasks) {
                String countHidden = preventionHiddenInfoRecordMapper.getTaskCountHidden(checkTask.getId());
                checkTask.setCountHidden(countHidden);
                PreventionHiddenInfoRecord hiddenInfoRecord = preventionHiddenInfoRecordMapper.getHiddenTaskDetails(checkTask.getCheckTaskConfigId(),checkTask.getId());
                if (!ObjectUtils.isEmpty(hiddenInfoRecord)){
                    checkTask.setCheckResult("有隐患");
                    this.preventionCheckTaskService.update(checkTask);
                }
            }
        }
        return getDataTable(preventionCheckTasks);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        PreventionCheckTaskVO taskInfoById = preventionCheckTaskService.getTaskInfoById(id);
        return AjaxResult.success(taskInfoById);
    }

    /**
     * 新增数据
     * @param preventionCheckTask 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody PreventionCheckTask preventionCheckTask) {
        return AjaxResult.success(this.preventionCheckTaskService.insert(preventionCheckTask));
    }

    /**
     * 编辑数据
     * @param preventionCheckTask 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PreventionCheckTask preventionCheckTask) {
        return AjaxResult.success(this.preventionCheckTaskService.update(preventionCheckTask));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.preventionCheckTaskService.deleteById(id));
    }

    /**
     * 修改任务状态
     * @param preventionCheckTask 实体
     * @return 编辑结果
     */
    @PostMapping("/updateTaskState")
    public AjaxResult updateTaskState(@RequestBody PreventionCheckTask preventionCheckTask) {
        preventionCheckTask.setCheckUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        preventionCheckTask.setCheckTime(DateUtil.now());
        preventionCheckTask.setTaskState("已排查");
        this.preventionCheckTaskService.updateTaskState(preventionCheckTask);
        return AjaxResult.success();
    }

    /**
     *  根据任务ID查询该任务详情信息
     * @param id
     * @return
     */
    @GetMapping("/getTaskInfoById")
    public AjaxResult getTaskInfoById(Integer id) {
        PreventionCheckTaskVO taskInfoById = preventionCheckTaskService.getTaskInfoById(id);
        return AjaxResult.success(taskInfoById);
    }

    /**
     *  查询登录用户的排查任务
     * @return
     */
    @GetMapping("/getTaskInfoByUserName")
    public TableDataInfo getTaskInfoByUserName(PreventionCheckTaskConfigDTO preventionCheckTaskConfigDTO) {
        startPage();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        preventionCheckTaskConfigDTO.setNickName(nickName);
        List<PreventionCheckTaskVO> preventionCheckTaskVOS = preventionCheckTaskService.queryByUserName(preventionCheckTaskConfigDTO);
        return getDataTable(preventionCheckTaskVOS);
    }


    /**
     *  查询履职责任人的排查记录
     * @return
     */
    @GetMapping("/getTaskInfoByDuty")
    public AjaxResult getTaskInfoByDuty(String startTime, String endTime) {
        Map<String,List<PreventionCheckTaskVO>> map = preventionCheckTaskService.getTaskInfoByDuty(startTime,endTime);
        return AjaxResult.success(map);
    }

    /**
     * 统计任务_日周月任务完成情况
     */
    @GetMapping("/countTaskDayWeekMonth")
    public AjaxResult countTaskDayWeekMonth(Integer tempType) {
        Calendar now = Calendar.getInstance();
        String yearTemp = String.valueOf(now.get(Calendar.YEAR));//年
        String monthTemp = String.valueOf(now.get(Calendar.MONTH) + 1);//月
        String dayTemp = String.valueOf(now.get(Calendar.DAY_OF_MONTH));//日
        DateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (monthTemp.length() <2){
            monthTemp = "0" + monthTemp;
        }
        List<PreventionCheckTaskCountVO> allTask =new ArrayList<>();
        if (1 == tempType){// 按日统计
            allTask = preventionCheckTaskService.getTaskDayTemp(yearTemp + "-" + monthTemp + "-" + dayTemp);
        }else if (2 == tempType){// 按周统计
            allTask = preventionCheckTaskService.getTaskMeekTemp(dateformat.format(getBeginDayOfWeek()),dateformat.format(getEndDayOfWeek()));
        }else if (3 == tempType){// 按月统计
            allTask = preventionCheckTaskService.getTaskMonthTemp(yearTemp + "-" + monthTemp);
        }else {// 全部
            allTask = preventionCheckTaskService.getallTask();
        }
        return AjaxResult.success(allTask);
    }



    /**
     * 统计隐患_日周月任务完成情况
     */
    @GetMapping("/countHiddenDayWeekMonth")
    public AjaxResult countHiddenDayWeekMonth(Integer tempType) {
        Calendar now = Calendar.getInstance();
        String yearTemp = String.valueOf(now.get(Calendar.YEAR));//年
        String monthTemp = String.valueOf(now.get(Calendar.MONTH) + 1);//月
        String dayTemp = String.valueOf(now.get(Calendar.DAY_OF_MONTH));//日
        DateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (monthTemp.length() <2){
            monthTemp = "0" + monthTemp;
        }
        List<PreventionCheckTaskCountVO> allHidden =new ArrayList<>();
        if (1 == tempType){// 按日统计
            allHidden = preventionHiddenRegisterService.getHiddenDayTemp(yearTemp + "-" + monthTemp + "-" + dayTemp);
        }else if (2 == tempType){// 按周统计
            allHidden = preventionHiddenRegisterService.getHiddenMeekTemp(dateformat.format(getBeginDayOfWeek()),dateformat.format(getEndDayOfWeek()));
        }else if (3 == tempType){// 按月统计
            allHidden = preventionHiddenRegisterService.getHiddenMonthTemp(yearTemp + "-" + monthTemp);
        }else {// 全部
            allHidden = preventionHiddenRegisterService.getAllHidden();
        }
        return AjaxResult.success(allHidden);
    }


    /**
     * 巡检任务_隐患数值穿透
     * @return 单条数据
     */
    @GetMapping("/queryPlanTaskId")
    public TableDataInfo queryPlanTaskId(Integer taskId) {
        startPage();
        List<PreventionHiddenRegister> preventionHiddenRegisters = new ArrayList<>();
        List<PreventionHiddenInfoRecord> recordList = this.preventionHiddenInfoRecordMapper.queryHiddenTaskId(taskId);
        if (!CollectionUtils.isEmpty(recordList)){
            for (PreventionHiddenInfoRecord infoRecord : recordList) {
                Integer hiddenId = infoRecord.getHiddenId();
                PreventionHiddenRegister preventionHiddenRegister = preventionHiddenRegisterService.queryById(hiddenId);
                if (!org.springframework.util.ObjectUtils.isEmpty(preventionHiddenRegister)){
                    preventionHiddenRegisters.add(preventionHiddenRegister);
                }
            }
        }
        return getDataTable(preventionHiddenRegisters);
    }


    /**
     * APP首页 我的任务
     * @return
     */
    @GetMapping("/getCountApp")
    public AjaxResult getCountApp() {

        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        PreventionCheckTask preventionCheckTask1 = new PreventionCheckTask();
        preventionCheckTask1.setCheckUserName(nickName);
        preventionCheckTask1.setTaskState("已排查");
        List<PreventionCheckTask> preventionCheckTasks1 = preventionCheckTaskMapper.queryAllByLimit(preventionCheckTask1);

        PreventionCheckTask preventionCheckTask2 = new PreventionCheckTask();
        preventionCheckTask2.setCheckUserName(nickName);
        preventionCheckTask2.setTaskState("已超时");
        List<PreventionCheckTask> preventionCheckTasks2 = preventionCheckTaskMapper.queryAllByLimit(preventionCheckTask2);

        PreventionCheckTask preventionCheckTask3 = new PreventionCheckTask();
        preventionCheckTask3.setCheckUserName(nickName);
        preventionCheckTask3.setTaskState("待排查");
        List<PreventionCheckTask> preventionCheckTasks3 = preventionCheckTaskMapper.queryAllByLimit(preventionCheckTask3);

        Map<String,Integer> map = new HashMap<>();
        map.put("wan", CollUtil.isEmpty(preventionCheckTasks1) ? 0 : preventionCheckTasks1.size());
        map.put("chao", CollUtil.isEmpty(preventionCheckTasks2) ? 0 : preventionCheckTasks2.size());
        map.put("dai", CollUtil.isEmpty(preventionCheckTasks3) ? 0 : preventionCheckTasks3.size());

        return AjaxResult.success(map);
    }
}

